#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_NEIGH	99
#define Q_LEN	100;

typedef enum e_bool 
{
	false,
	true
} bool;

typedef struct s_queue
{
	int head;
	int tail;
	int max;
	int size;
	void **tab;
} queue;

void queue_create(queue **q, int n)
{
//	printf("create ENTER\n");
	
	*q = malloc(sizeof(queue));
	(*q)->tab = malloc(sizeof(void*) * n);
	(*q)->head = 0;
	(*q)->tail = 0;
	(*q)->size = 0;
	(*q)->max = n;
	
//	printf("create EXIT\n");
}

void queue_enq(queue *q, void *v)
{
	if(q->size >= q->max) return;
	q->tab[q->head] = v;
	q->head = (q->head + 1) % q->max;
//	printf("d:%d\n",*((int*)v));
	q->size++;
}

void *queue_deq(queue *q)
{
	if(q->size <= 0) return NULL;
	void *ret = q->tab[q->tail];
	q->tail = (q->tail + 1) % q->max;
	q->size--;
	return ret;
}

void queue_print_int(queue *q)
{
	int i;
	for(i = 0; i < q->size; i++)
	{
		if(q->tab[(q->tail + i) % q->max] == NULL)
			printf("%d DUPA!\n", i);
		else
			printf("%d ", *((int*)(q->tab[(q->tail + i) % q->max])));
	}
}

void queue_enq_int(queue *q, int n)
{
	int *item = malloc(sizeof(int));
	*item = n;
	queue_enq(q, item);
}

int main()
{
	queue *q;
	queue_create(&q, 10);
	
	printf("1:\t");
	
	queue_enq_int(q, 1);
	queue_enq_int(q, 2);
	queue_enq_int(q, 3);
	queue_enq_int(q, 4);
	queue_enq_int(q, 5);
	
	queue_print_int(q);
	printf("\n2:\t");	

	queue_deq(q);
	queue_deq(q);
	queue_deq(q);
	
	queue_print_int(q);
	printf("\n3:\t");	

	queue_enq_int(q, 6);
	queue_enq_int(q, 7);
	queue_enq_int(q, 8);
	queue_enq_int(q, 9);
	queue_enq_int(q, 10);

	queue_print_int(q);
	printf("\n4:\t");	

	queue_deq(q);
	queue_deq(q);
	queue_deq(q);

	queue_enq_int(q, 11);
	queue_enq_int(q, 12);
	queue_enq_int(q, 13);
	queue_enq_int(q, 14);
	queue_enq_int(q, 15);

	queue_print_int(q);
	printf("\n5:\t");	

	queue_deq(q);
	queue_deq(q);
	queue_deq(q);

	queue_enq_int(q, 11);
	queue_enq_int(q, 12);
	queue_enq_int(q, 13);
	queue_enq_int(q, 14);
	queue_enq_int(q, 15);

	queue_print_int(q);
	printf("\n");	

	return 0;
}

